#include "proprocessReq.h"
#include "func.h"
#include "pathStack.h"
#include "transMsg.h"
#include "token.h"

int recvRequest(int netfd,char* request){
    train_t train;
    memset(&train,0,sizeof(train_t));
    int ret = recvn(netfd,&train.length,sizeof(train.length));
    if(ret == 1){
        return -1;
    }
    /* printf("train.length = %d",train.length); */
    ret = recvn(netfd,request,train.length);
    if(ret == 1){
        return -1;
    }
    /* printf("train.data = %s\n",train.data); */
    return 0;
}

int tokenVerify(const char* id,const char* token){
    char jwt[1024] ={0};
    int ret = generateToken(id,jwt);
    /* printf("token = %s\n",jwt); */
    /* printf("jwt = %s\n",jwt); */
    if(ret == -1){
        printf("generate token Failed!\n");
    }else{
        if(memcmp(jwt,token,strlen(jwt)+1)==0){
            //token验证成功
            return 0;
        }
    }
    return -1;
}

int fieldSeparation(const char* request,char* firstPara,char* secondPara,char* thirdPara){
    //切分命令和参数
    char tempRequest[4096] = {0};
    memcpy(tempRequest,request,strlen(request)+1);
    /* printf("tempRequest = %s\n",tempRequest); */
    int arglen;
    arglen = 0;
    char* saveptr;
    char* reqFirst = strtok_r(tempRequest," ",&saveptr); //第一个字段
    if(reqFirst!=NULL){
        memcpy(firstPara,reqFirst,strlen(reqFirst)+1);
        char* reqSecond = strtok_r(NULL," ",&saveptr);  //第二个字段
        if(reqSecond!=NULL){
            memcpy(secondPara,reqSecond,strlen(reqSecond)+1);
            if(saveptr != NULL){
                memcpy(thirdPara,saveptr,strlen(saveptr)+1);   //剩余的为第三个字段
                /* printf("first = %s,second = %s,third = %s\n",reqFirst,reqSecond,saveptr); */
            }

        }
    }
    return 0;
}
